package ch.fhnw.qualiscan.application.modals;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.views.AdminPanelView;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.helpers.adminRoleTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Permission;
import ch.fhnw.qualiscan.core.db.pojos.Role;
import ch.fhnw.qualiscan.core.db.pojos.RoleText;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class AdminEditRole extends Modal {
	
	
	private adminRoleTableArgs arta;
	private Form editForm;
	private AdminModel adminModel;
	private List<Language> langList;
	private AdminPanelView caller;
	
	public AdminEditRole(adminRoleTableArgs arta, AdminPanelView caller) {
		setCaption(TM.get("admin.editroles.title"));
		this.arta = arta;
		this.caller = caller;
		
		adminModel = new AdminModel();
		langList = adminModel.getLanguages();
		buildLayout();
	}
	
	public void buildLayout() {
		editForm = new Form();
		HorizontalLayout buttonBar = new HorizontalLayout();
		editForm.getFooter().addComponent(buttonBar);
		editForm.setCaption(TM.get("admin.editroles.title"));
		
		for(int i = 0; i < langList.size(); i++) {

			TextField titleField = new TextField(); 
			titleField.setColumns(20);
			titleField.setWidth("100%");
			titleField.setValue(adminModel.getRoleText(arta.getRole(), langList.get(i)).getTitle());
			titleField.setCaption(TM.get("admin.editdomain.domaintitle") + " " + langList.get(i).getLanguage());
			editForm.addField("title"+langList.get(i).getId(), titleField);
			
			TextField descriptionField = new TextField();
			descriptionField.setWidth("100%");
			descriptionField.setRows(3);
			descriptionField.setColumns(20);
			descriptionField.setValue(adminModel.getRoleText(arta.getRole(), langList.get(i)).getDescription());
			descriptionField.setCaption(TM.get("admin.editdomain.domaindesc") + " " + langList.get(i).getLanguage());
			editForm.addField("desc"+langList.get(i).getId(), descriptionField);
			
			editForm.addField(null, null);
		}
		
		List<Permission> permissionList = adminModel.getPermissions();
		
		NativeSelect permissionSelect = new NativeSelect(TM.get("admin.newrole.selectpermission"));
		
		for(int p = 0; p < permissionList.size(); p++) {
			permissionSelect.addItem(permissionList.get(p));
			permissionSelect.setItemCaption(permissionList.get(p), permissionList.get(p).getTitle());
			if (permissionList.get(p).getId() == arta.getRole().getPermissions().getId()) {
				permissionSelect.setValue(permissionList.get(p));
			}
		}
		
		 permissionSelect.setNullSelectionAllowed(false);
	     permissionSelect.setImmediate(true);
	    
	    editForm.addField("permission", permissionSelect);
		
		Button saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				editForm.commit();
				boolean rebuild = false;
				arta.getRole().setPermissions((Permission) editForm.getField("permission").getValue());
				for (int i = 0; i < langList.size(); i++) {
					if ((String)editForm.getField("title"+langList.get(i).getId()).getValue() != "") {
						if (adminModel.existsRoleText(arta.getRole(), langList.get(i))) {
							RoleText rtxt = adminModel.getRoleText(arta.getRole(), langList.get(i));
							rtxt.setTitle((String) editForm.getField("title"+langList.get(i).getId()).getValue());
							rtxt.setDescription((String) editForm.getField("desc"+langList.get(i).getId()).getValue());
							
							if (langList.get(i).getId() == Config.ENGLISH_ID) {
								rebuild = true;
							}
							hibernateSession.getSession().flush();
						}
						else {
							
							rebuild = true;
							
							RoleText rtxt = new RoleText();
							rtxt.setLanguage(langList.get(i));
							rtxt.setTitle((String) editForm.getField("title"+langList.get(i).getId()).getValue());
							rtxt.setDescription((String) editForm.getField("desc"+langList.get(i).getId()).getValue());
							hibernateSession.getSession().save(rtxt);
							
							Role r = (Role) hibernateSession.getSession().get(Role.class, arta.getRole().getId());
							r.addRoletex(rtxt);
							hibernateSession.getSession().flush();
						}
					}
				}
				
				hibernateSession.closeSession();
				
				if (rebuild == true) {
					caller.updateRoles();
				}
				AdminEditRole aed = (AdminEditRole) event.getButton().getData();
				aed.close();
			}
		});
		saveButton.setIcon(Config.ICON_SAVE);
		saveButton.setData(this);
		
		buttonBar.addComponent(saveButton);
		
		addComponent(editForm);
		
	}
}

